﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;

namespace RedPanda.MusicPlayer
{
    class MusicService
    {
        private static string API_KEY = "ab68a9dd177311dde064b43cca33c342";
        public static string getAlbumInfo(string artistName, string albumName)
        {
            string method = "album.getInfo";
            string query = "";
            if (artistName == null || artistName.Length == 0 || albumName == null || albumName.Trim().Length == 0)
            {
                return "";
            }
            else
            {
                query = "album=" + encode(albumName) + "&artist=" + encode(artistName);
            }
            XmlDocument xmlCH = send(method, API_KEY, "zh", query);
            if (xmlCH.GetElementsByTagName("error").Count != 0)
            {
                return "";
            }
            if (xmlCH.GetElementsByTagName("summary").Count != 0 && xmlCH.GetElementsByTagName("summary")[0].InnerText.Length != 0)
            {
                return xmlCH.OuterXml;
            }
            XmlDocument xmlEN = send(method, API_KEY, "en", query);
            return xmlEN.OuterXml;
        }

        public static string getArtistInfo(string artistName)
        {
            string method = "artist.getInfo";
            string query = "artist=" + encode(artistName);
            XmlDocument xmlCH = send(method, API_KEY, "zh", query);
            if (xmlCH.GetElementsByTagName("error").Count != 0)
            {
                return "";
            }
            if (xmlCH.GetElementsByTagName("summary").Count != 0 && xmlCH.GetElementsByTagName("summary")[0].InnerText.Length != 0)
            {
                return xmlCH.OuterXml;
            }
            XmlDocument xmlEN = send(method, API_KEY, "en", query);
            return xmlEN.OuterXml;
        }
        public static string getTrackInfo(string artistName, string trackName)
        {
            if (artistName == null || artistName.Length == 0 || trackName == null || trackName.Trim().Length == 0)
            {
                return "";
            }
            string method = "track.getInfo";
            string query = "artist=" + encode(artistName) + "&track=" + encode(trackName);
            XmlDocument xmlCH = send(method, API_KEY, "zh", query);
            if (xmlCH.GetElementsByTagName("error").Count != 0)
            {
                return "";
            }
            if (xmlCH.GetElementsByTagName("summary").Count != 0 && xmlCH.GetElementsByTagName("summary")[0].InnerText.Length != 0)
            {
                return xmlCH.OuterXml;
            }
            XmlDocument xmlEN = send(method, API_KEY, "en", query);
            return xmlEN.OuterXml;
        }

        public static string getLyric(string artistName, string filename)
        {
            artistName = lyricEncode(artistName);
            filename = lyricEncode(filename);
            string uri = "http://lyricwiki.org/api.php?func=getSong&artist=" + artistName + "&song=" + filename + "&fmt=xml";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            XmlDocument xml = new XmlDocument();
            xml.Load(response.GetResponseStream());
            response.GetResponseStream().Close();
            response.Close();
            if (xml.GetElementsByTagName("lyrics").Count == 0)
            {
                return "Not found";
            }
            return xml.GetElementsByTagName("lyrics")[0].InnerText;

        }

        private static XmlDocument send(string method, string apikey, string lang, string query)
        {
            string uri = "http://ws.audioscrobbler.com/2.0/?lang=" + lang + "&method=" + method + "&api_key=" + apikey + "&" + query;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            XmlDocument xml = new XmlDocument();
            xml.Load(response.GetResponseStream());
            response.Close();
            return xml;
        }
        private static string encode(string str)
        {

            string result = System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
            return result;

        }
        private static string lyricEncode(string str)
        {
            string result = str.Replace(' ', '_');
            result = System.Web.HttpUtility.UrlEncode(result, Encoding.UTF8);
            return result;
        }
    }
}
